package com.example.dicos.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 计算时间差
 */
public class TimeStamp {
    /**
     * 分钟时间戳
     */
    public static Long minute() {
        long minute = (1000 * 60 * 30);
        return minute;
    }

    /**
     * 返回文字串
     *
     * @param milliseconds
     * @return
     */
    public static String timeStampToString(long milliseconds) {
        long day = TimeUnit.MILLISECONDS.toDays(milliseconds);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day + "天");
        }
        sb.append(hours + "小时");
        sb.append(minutes + "分");
        sb.append(seconds + "秒");
        return sb.toString();
    }

    /**
     * 返回数字串
     *
     * @param milliseconds
     * @return
     */
    public static String timeStampToNumber(long milliseconds) {
        long day = TimeUnit.MILLISECONDS.toDays(milliseconds);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day + "天");
        }
        sb.append(hours + ".");
        sb.append(minutes);
        return sb.toString();
    }


    /**
     * 毫秒数
     *
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static long calLastedTime(String startTime, String endTime) throws ParseException {
        // 取出开始时间的毫秒数
        long newTime = stringToDate(startTime).getTime();
        // 取出结束时间的毫秒数
        long oldTime = stringToDate(endTime).getTime();
        // 计算两个时间的时间差
        long diff = Math.abs(newTime - oldTime);
        return diff;
    }

    /**
     * 时间格式
     *
     * @param date
     * @return
     * @throws ParseException
     */
    protected static Date stringToDate(String date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(date);
    }
}
